$.ajaxSetup({contentType:"application/json;charset=gbk"});                

var dept = $("select#dept");
if(dept && dept.children() && dept.children().length<2){  
    $.getJSON("dept.asp",function(d){
        if(d && d.data )
            $.each(d.data, function( k, v){ 
                $("select#dept").append($("<option></option>").val(v.ID).html(v.部门名称))
            })
    });
}
function getData(){
    $("#form").hide();
    $("#info").show();
    var input={};    
    input["DealType"] = $("select#Type").val();
    input["deptID"] = $("select#dept>option:selected").val();
    input["deptName"] = $("select#dept>option:selected").text();
    if( $("input[name=TimeS2]").val() == "00:00:01" || $("input[name=TimeS2]").val() == "00:00"){
        input["DealTimeS"] = $("input[name=TimeS1]").val() +" 00:00:00"
    }else{
        input["DealTimeS"] = $("input[name=TimeS1]").val() +" " + $("input[name=TimeS2]").val()
    }
    if( $("input[name=TimeE2]").val() == "00:00"){
        input["DealTimeE"] = $("input[name=TimeE1]").val() +" 00:00:00"
    }else{
        input["DealTimeE"] = $("input[name=TimeE1]").val() +" " + $("input[name=TimeE2]").val()
    }
    $.get("json.asp",input,json2pdf);
}

var doc1 = new jsPDF('p', 'pt', 'a4');// 获取实例
console.log(doc1);
doc1.setProperties({
    title: '各部门补贴情况汇总',
    subject: '',
    author: 'showcai',
    keywords: '各部门补贴情况汇总',
    creator: 'showcai'
});
doc1.addFileToVFS('font.ttf', moo);
doc1.addFont('font.ttf', 'custom', 'normal');
doc1.setFont('custom');//字体
doc1.setFontType("normal");//样式

function json2pdf(json){
    var arr = (JSON.parse(json));
    var check = arr.check ;
    if(check['条件']){  check['条件'] = '统计条件：' + check['条件']  }else{ check['条件'] = '' };
    if(check['时间']){  check['时间'] = '生成时间：' + check['时间']  }else{ check['时间'] = '' };
    if(check['总计次']){  check['总计次'] = '总计次：' + check['总计次']  }else{ check['总计次'] = '' };
    if(check['总金额']){  check['总金额'] = '总金额：' + check['总金额']  }else{ check['总金额'] = '' };
    doc1.autoTable({                   // 这是动态获取后台table数据数据导出
        columns: [       // 可以传参
            { header: '\r\n部门名称', dataKey: '部门名称' },
            { header: '早餐消费次数', dataKey: '早餐消费次数' },
            { header: '午餐消费次数', dataKey: '午餐消费次数' },
            { header: '晚餐消费次数', dataKey: '晚餐消费次数' },
            { header: '夜宵消费次数', dataKey: '夜宵消费次数' },
            { header: '早餐\r\n\r\n补贴金额', dataKey: '早餐补贴金额' },
            { header: '午餐\r\n\r\n补贴金额', dataKey: '午餐补贴金额' },
            { header: '晚餐\r\n\r\n补贴金额', dataKey: '晚餐补贴金额' },
            { header: '夜宵\r\n\r\n补贴金额', dataKey: '夜宵补贴金额' },
            { header: '\r\n消费总金额', dataKey: '消费总金额' },
            { header: '\r\n补贴总金额', dataKey: '补贴总金额' }
        ],
        body: arr.data ,
        styles: { fillColor: [255, 255, 255], font: 'custom', textColor: [0,0,0] },   // 样式配置，有些类似canvas
        theme: 'grid',
        styles: {
              overflow: 'linebreak',
              font: 'custom', // 设置中文
              fontSize: 10,//默认字体大小
              color: '#000',
              halign:"center"
        },
        columnStyles: {
              0: { cellWidth: 60,fontSize:8,fontWeight:'bold',halign:"center",textColor:'#333383',},
              1: { cellWidth: 30,fontSize:8,fontWeight:'bold',halign:"center",},
              2: { cellWidth: 30,fontSize:8,fontWeight:'bold',halign:"center",},
              3: { cellWidth: 30,fontSize:8,fontWeight:'bold',halign:"center",},
              4: { cellWidth: 30,fontSize:8,fontWeight:'bold',halign:"center",},
              5: { cellWidth: 50,fontSize:8,fontWeight:'bold',halign:"center",fillColor: 240,},
              6: { cellWidth: 50,fontSize:8,fontWeight:'bold',halign:"center",fillColor: 240,},
              7: { cellWidth: 50,fontSize:8,fontWeight:'bold',halign:"center",fillColor: 240,},
              8: { cellWidth: 50,fontSize:8,fontWeight:'bold',halign:"center",fillColor: 240,},
              9: { cellWidth: 66,fontSize:8,fontWeight:'bold',halign:"center",},
              10: { cellWidth: 66,fontSize:8,fontWeight:'normal',halign:"center",fillColor: 240,valign:'middle',}
        },
        didDrawCell: (data) => { 
            // 在渲染完单元格后的事件
            //console.log(data.column.raw,data.cell.raw,data);
        },
        didParseCell: (HookData) => {
            //  当插件完成解析单元格内容时调用
        },     
        willDrawCell: (HookData) => {
            //   在绘制单元格或行之前调用
        },
        willDrawPage: (HookData) => {
        //   插件在页面上绘制之前调用
        },
        didDrawPage: (HookData) => {
            doc1.text( '各部门补贴情况汇总',setTextCenter(doc1,9), 30)      //  渲染title
            doc1.setFontSize(8);//字体大小
            doc1.text( check['条件'] , 50, 810, 0, 'left' );//左对齐
            doc1.text( check['总计次'] , 50, 820, 0, 'left' );//右对齐
            doc1.text( check['总金额'] , 150, 820, 0, 'left' );//右对齐
            doc1.text( check['时间'] , 280, 820, 0, 'left' );//右对齐
            doc1.setFontSize(10);//字体大小
            var page = "第 " + (HookData.pageNumber) + " 页";
            doc1.text( page , 545, 820, 0, 'right' );//右对齐
            //   插件在页面上绘制完所有内容后调用
        },
    });
    $("#info").hide();
    document.getElementById("frame").style='height: 820px;';
    document.getElementById("output").src = doc1.output('datauristring');   // 渲染pdf
}

function setTextCenter (doc, txtLength) {
    var fontSize = doc.getFontSize()
    // Get page width
    var pageWidth = doc.internal.pageSize.width
    const txtWidth = txtLength * fontSize / doc.internal.scaleFactor
    return (pageWidth - txtWidth) / 2
}
	


  
